-- invalid_skill
-- created by chendh
-- 受创使自己技能无效化

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 受创触发
    trigger = function(who, prop, arg)
        local target = arg;

        -- 判断攻击方的系别是否符合要求
        if target.dbase:query("style", 0) ~= tonumber(prop[2]) then
            return 0;
        end

        local skill_id = prop[3];
        local max_times = 1;
        local skill_dbase = SkillM.query(skill_id, "dbase");
        -- 默认攻击1次可以使技能无效，如果有配置，则用表里配置的数值
        if type(skill_dbase) == "table" and skill_dbase["attacked_times"] then
            max_times = skill_dbase["attacked_times"];
        end

        -- 获取累计攻击次数
        local attacked_times = who.dbase:query("invalid_skill_attacked_times") or 0;
        attacked_times = attacked_times + 1;
        who.dbase:set("invalid_skill_attacked_times", attacked_times);

        if attacked_times >= max_times then
            -- 使技能无效化
            local skills = who.dbase:query("skills");

            if table.indexOf(skills, skill_id) >= 0 then
                -- 根据需求弹出提示
                if skill_dbase["invalid_tip"] then
                    doAlert(skill_dbase["invalid_tip"]);
                end

                -- 删除失效的技能后刷新下属性
                table.removeItem(skills, skill_id);

                who.dbase:set("skills", skills);
                PropM.refresh(who);

                -- 记录失效的技能
                local invalid_skills = who.dbase:query("invalid_skills") or {};
                table.insert(invalid_skills, skill_id);
                who.dbase:set("invalid_skills", invalid_skills);

                -- 抛出技能无效化事件
                EventMgr.fire(event.INVALID_SKILL, { ["skill_id"] = skill_id, ["monster"] = who, });
            end
        else
            -- 根据需求弹出提示
            if skill_dbase["attacked_tip"] then
                doAlert(skill_dbase["attacked_tip"]);
            end
        end

        return 0;
    end,

    record = function(target, prop, path)
        return false;
    end
};
